package za.co.ray.oxm.api;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;

public abstract class AbstractXMLConverter {

    private Marshaller marshaller;

    private Unmarshaller unmarshaller;

    public Marshaller getMarshaller() {
        return marshaller;
    }

    public void setMarshaller(Marshaller marshaller) {
        this.marshaller = marshaller;
    }

    public Unmarshaller getUnmarshaller() {
        return unmarshaller;
    }

    public void setUnmarshaller(Unmarshaller unmarshaller) {
        this.unmarshaller = unmarshaller;
    }

    public void convertFromObjectToXML(Object object, OutputStream out) throws IOException {
        getMarshaller().marshal(object, new StreamResult(out));
    }

    public Object convertFromXMLToObject(InputStream is) throws IOException {
        return getUnmarshaller().unmarshal(new StreamSource(is));
    }

}
